package com.chazz.servlet;/**
 *
 */

import com.alibaba.fastjson.JSONArray;
import com.chazz.entity.Category;
import com.chazz.service.CategoryidService;
import com.chazz.service.impl.CategoryidServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 *
 * @title: CategoryidServlet
 * @Author ZhaoHongRui
 * @Date: 2020/9/21 16:02
 * @Version 1.0
 */

@WebServlet("/category.do")
public class CategoryServlet extends HttpServlet {
    CategoryidService categoryidService = new CategoryidServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("application/json;charset=UTF-8");
        String method = req.getParameter("method");
        PrintWriter out = resp.getWriter();
        if (method.equals("search")) {
            List<Category> categoryList = categoryidService.getCategoriesList(req.getParameter("searchValue"));
            String categoryListToJson = JSONArray.toJSONString(categoryList);
            out.write(categoryListToJson);
        } else if (req.getParameter("md").equals("categorySearch")) {

            String categoryname = req.getParameter("categoryname");
            System.out.println(categoryname);


        }

    }
}
